<template>
  <div>
    <span>停车场会员消费情况</span>
    <!-- 搜索筛选 -->
    <el-form  :inline="true"  :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.auUsername" placeholder="输入用户账号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <!--<el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>-->
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="poNo" label="订单编号">
      </el-table-column>
      <el-table-column sortable prop="parkingLotVO.plName" label="停车场名称">
      </el-table-column>
      <el-table-column sortable prop="appUserVO.auUsername" label="用户账号">
      </el-table-column>
      <el-table-column sortable prop="poPrice" label="付钱金额">
      </el-table-column>

      <el-table-column sortable prop="poStart" label="开始时间" :formatter="dateFormat">
      </el-table-column>
      <el-table-column sortable prop="poEnd" label="到期时间" :formatter="dateFormat">
      </el-table-column>
      <el-table-column sortable prop="poState" label="订单状态" :formatter="formatStatus">
      </el-table-column>
      <!--<el-table-column align="center" label="操作">-->
      <!--<template slot-scope="scope">-->
      <!--<el-button size="mini" @click="editVipUser(scope.$index, scope.row)">编辑</el-button>-->
      <!--Enable  Disable-->
      <!--<el-button size="mini" type="success" @click="EnableVipUser(scope.$index, scope.row)">启用</el-button>-->
      <!--<el-button size="mini" type="danger" @click="DisableVipUser(scope.$index, scope.row)">禁用</el-button>-->
      <!--</template>-->
      <!--</el-table-column>-->
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="用户名" prop="auUsername">
          <el-input size="small" v-model="editForm.auUsername" auto-complete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="Vip类型" prop="vtName">
          <el-input size="small" v-model="editForm.vtName" auto-complete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="uvStart">
          <div class="block">
            <el-date-picker
              v-model="editForm.uvStart"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              align="right"
              :picker-options="pickerOptions"
              :disabled="true">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="到期时间" prop="uvEnd">
          <div class="block">
            <el-date-picker
              v-model="editForm.uvEnd"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              align="right"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="uvState">
          <el-input size="small" v-model="editForm.uvState"></el-input>
          <span>[0有效][1过期]</span>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitEditUserVip">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../../components/Pagination';
import moment from 'moment';
// import {getVipUsers} from "../../http/api";
// import {getPKVipUserOrders,} from "../../http/api";
// import {submitEditUserVip,updateUserVipState} from '../../http/api'
import {getPKVipUserOrders,submitEditUserVip,updateUserVipState,getPKVipUserOrder} from '../../api/vipMG'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        auId: '',
        auUsername: '',
        vtName: '',
        deptNo: '',
        uvStart: '',
        uvEnd: '',
        uvState: '',
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
      formInline: {
        page: 1,
        limit: 5,
        varLable: '',
        varName: '',
        auUsername: '',
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 4,
        total: 10
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 创建完毕
   */
  created() {
    // this.getVipUsers(this.formInline);
    this.getPKVipUserOrders(this.formInline);
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    formatStatus(row, column, cellValue, index){
      return cellValue==0?'待支付':cellValue==1?'已支付':'未知';
    },
    dateFormat(row, column, cellValue, index){
      return moment(cellValue).format("YYYY-MM-DD HH:mm:ss")
    },


    getVipUsers(userVip) {
      this.loading = true;
      // console.log("userVip:",userVip);
      getVipUsers(userVip)
        .then(response => {
          let result = response.data;
          this.loading = false;
          if (result.code == 0) {
            this.listData = result.data.list;
            // console.log("result.data:",result.data);
            // 分页赋值
            // this.pageparm.currentPage = this.formInline.page;
            // this.pageparm.pageSize = this.formInline.limit;
            // this.pageparm.total = result.data.total;
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = result.data.total;
          } else {
            this.$message({
              type: 'info',
              message: result.msg,
            })
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error('用户vip列表加载失败，请稍后再试！')
        })
    },
    //getPKVipUserOrders
    getPKVipUserOrders(parkingOrder) {
      this.loading = true;
      // console.log("userVip:",userVip);
      getPKVipUserOrders(parkingOrder)
        .then(response => {
          // let result = response.data;
          let result = response;
          this.loading = false;
          if (result.code == 0) {
            this.listData = result.data.list;
            // console.log("result.data:",result.data);
            // 分页赋值
            // this.pageparm.currentPage = this.formInline.page;
            // this.pageparm.pageSize = this.formInline.limit;
            // this.pageparm.total = result.data.total;
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = result.data.total;
          } else {
            this.$message({
              type: 'info',
              message: result.msg,
            })
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error('用户vip列表加载失败，请稍后再试！')
        })
    },



    getPKVipUserOrder(parkingOrder) {
      this.loading = true;
      // console.log("userVip:",userVip);
      getPKVipUserOrder(parkingOrder)
        .then(response => {
          // let result = response.data;
          let result = response;
          this.loading = false;
          if (result.code == 0) {
            this.listData = result.data.list;
            // console.log("result.data:",result.data);
            // 分页赋值
            // this.pageparm.currentPage = this.formInline.page;
            // this.pageparm.pageSize = this.formInline.limit;
            // this.pageparm.total = result.data.total;
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = result.data.total;
          } else {
            this.$message({
              type: 'info',
              message: result.msg,
            })
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error('用户vip列表加载失败，请稍后再试！')
        })
    },



    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getPKVipUserOrders(this.formInline);
    },
    // 搜索事件
    search() {
      // this.getVipUsers(this.formInline);
      // this.getPKVipUserOrders(this.formInline);
      this.getPKVipUserOrder(this.formInline);
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != 'undefined') {
        this.title = '修改';
        this.editForm.deptId = row.deptId;
        this.editForm.deptName = row.deptName;
        this.editForm.deptNo = row.deptNo
      } else {
        this.title = '添加';
        this.editForm.deptId = '';
        this.editForm.deptName = '';
        this.editForm.deptNo = ''
      }
    },


    editVipUser: function(index, row) {
      // console.log("row::",row);
      this.editFormVisible = true;
      this.editForm=row;
      let appUserVO =row.appUserVO;
      // console.log("row.appUserVO",row.appUserVO);
      this.editForm.auUsername=appUserVO.auUsername;
      this.editForm.vtName=row.vipTypeVO.vtName;
    },

    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          deptSave(this.editForm)
            .then(res => {
              this.editFormVisible = false;
              this.loading = false;
              if (res.success) {
                this.getdata(this.formInline);
                this.$message({
                  type: 'success',
                  message: '公司保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('公司保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },

    submitEditUserVip() {
      // console.log("this.editForm:",this.editForm);
      submitEditUserVip(this.editForm).then(response=>{
        let result = response.data;
        if (result.code==0){
          this.editFormVisible = false;
          this.getVipUsers();
          this.$message({
            type: 'success',
            message: 'Vip用户的信息修改成功！'
          });
        } else if (result.code==1) {
          this.editFormVisible = true;
          this.getVipUsers();
          this.$message({
            type: 'error',
            message: 'Vip用户的信息修改失败！'
          });
        }
      }).catch(err => {
        this.editFormVisible = false;
        this.loading = false;
        this.getVipUsers();
        this.$message.error('修改失败，请稍后再试！');
      });
    },
    // 启用
    EnableVipUser(index, row) {
      let vipUser={};
      vipUser.uvId=row.uvId;
      vipUser.uvState=0;
      console.log("row0:",row);
      console.log("vipUser0:",vipUser);
      updateUserVipState(vipUser).then(response=>{
        let result = response.data;
        if (result.code==0){
          this.getVipUsers();
          this.$message({
            type: 'success',
            message: result.msg,
          });
        } else if (result.code==1) {
          this.getVipUsers();
          this.$message({
            type: 'error',
            message: result.msg,
          });
        }
      }).catch(err => {
        this.editFormVisible = false;
        this.loading = false;
        this.getVipUsers();
        this.$message.error('启用失败，请稍后再试！');
      });
    },

    DisableVipUser(index, row) {
      let vipUser={};
      vipUser.uvId=row.uvId;
      vipUser.uvState=1;
      console.log("row1:",row);
      console.log("vipUser1:",vipUser);
      updateUserVipState(vipUser).then(response=>{
        let result = response.data;
        if (result.code==0){
          this.getVipUsers();
          this.$message({
            type: 'success',
            message: result.msg,
          });
        } else if (result.code==1) {
          this.getVipUsers();
          this.$message({
            type: 'error',
            message: result.msg,
          });
        }
      }).catch(err => {
        this.editFormVisible = false;
        this.loading = false;
        this.getVipUsers();
        this.$message.error('启用失败，请稍后再试！');
      });
    },

    deleteUser(index, row) {

    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>
